package com.ruoyi.ora.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.enums.dict.AiModel;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.ora.constant.BizConstants;
import com.ruoyi.ora.domain.ChatRequest;
import com.ruoyi.ora.domain.ChatResponse;
import com.ruoyi.ora.service.IAiInterfaceService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Service
public class ChatHunyuanServiceImpl implements IAiInterfaceService {

    private RestTemplate restTemplate = new RestTemplate();

    private ISysConfigService sysConfigService = SpringUtils.getBean(ISysConfigService.class);

    @Override
    public String chatCompletions(String prompt) {
        return getChatCompletions(prompt).getChoices().get(0).getMessage().getContent();
    }

    @Override
    public ChatResponse getChatCompletions(String prompt) {
        return getChatCompletions(prompt, AiModel.HUNYUAN);
    }

    @Override
    public ChatResponse getChatCompletions(String prompt, AiModel aiModel) {
        ChatRequest chatRequest = new ChatRequest();
        chatRequest.setModel(aiModel.getValue());
        List<ChatRequest.Message> messages = new ArrayList<>();
        ChatRequest.Message message = new ChatRequest.Message();
        message.setRole("user");
        message.setContent(prompt);
        messages.add(message);
        chatRequest.setMessages(messages);

        return chatCompletions(chatRequest);
    }

    @Override
    public ChatResponse chatCompletions(ChatRequest chatRequest) {
        String url = sysConfigService.selectConfigByKey(BizConstants.Redis.SysConfig.HUNYUAN_API_URL); // "https://api.hunyuan.cloud.tencent.com/v1/chat/completions";
        String apiKey = sysConfigService.selectConfigByKey(BizConstants.Redis.SysConfig.HUNYUAN_API_KEY);  // "sk-YSM73W3NgLSTFifGwQ8vapXwYfGhphD6poTuKElvwcWOP4dp";

        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer "+apiKey);
        headers.setContentType(MediaType.APPLICATION_JSON); // 如果需要设置内容类型
        HttpEntity<Object> entity = new HttpEntity<>(JSONObject.toJSONString(chatRequest), headers);
        ChatResponse chatResponse = restTemplate.postForObject(url, entity, ChatResponse.class);
        return chatResponse;
    }

    public static void main(String[] args) {

        ChatRequest chatRequest = new ChatRequest();
        chatRequest.setModel("hunyuan-turbos-latest");
        List<ChatRequest.Message> messages = new ArrayList<>();
        ChatRequest.Message message = new ChatRequest.Message();
        message.setRole("user");
        message.setContent("用一句话解释量子计算");
        messages.add(message);
        chatRequest.setMessages(messages);


        String url = "https://api.hunyuan.cloud.tencent.com/v1/chat/completions";
        String apiKey = "sk-YSM73W3NgLSTFifGwQ8vapXwYfGhphD6poTuKElvwcWOP4dp";
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer "+apiKey);
        headers.setContentType(MediaType.APPLICATION_JSON); // 如果需要设置内容类型
        HttpEntity<Object> entity = new HttpEntity<>(JSONObject.toJSONString(chatRequest), headers);
        RestTemplate restTemplate = new RestTemplate();
        ChatResponse chatResponse = restTemplate.postForObject(url, entity, ChatResponse.class);

        System.out.println(chatResponse.getChoices().get(0).getMessage().getContent());
        System.out.println(chatResponse);
    }
}
